package com.ibm.risk.irmp.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.springframework.scheduling.support.CronSequenceGenerator;

import java.util.Calendar;
import java.util.Date;

public class CronUtil {
    private final Logger log = LoggerFactory.getLogger(getClass());

//    public static Long nextTime(String cronStr, Long currentT) {
//        CronSequenceGenerator generator = new CronSequenceGenerator(cronStr);
//        if (currentT == null) {
//            return generator.next(new Date()).getTime();
//        } else {
//            return generator.next(new Date(currentT)).getTime();
//        }
//    }

    public static String transferToCronString(String policyStr) {
        String retStr = "0 ";

        String[] ts = policyStr.split(";");
        switch (ts[0]) {
            case "minute":
                retStr += ts[1] + "* * * * *";
                break;
            case "hour":
                retStr += "0 " + ts[1] + " * * * *";
                break;
            case "day":
                retStr += "0 " + ts[2] + " " + ts[1] + " * * *";
                break;
            case "week":
                retStr += "0 " + ts[3] + " " + ts[2] + " ? * " + ts[1];
                break;
            case "year":
                retStr += "0 " + ts[4] + " " + ts[3] + " " + ts[2] + " " + ts[1] + " ?";
                break;
            default:
                retStr = "";
                break;
        }

        return retStr;
    }

    //Cron: Seconds Minutes Hours DayofMonth Month DayofWeek Year
    /*
    字段	允许值	允许的特殊字符
    秒（Seconds）	0~59的整数	, - * /    四个字符
    分（Minutes）	0~59的整数	, - * /    四个字符
    小时（Hours）	0~23的整数	, - * /    四个字符
    日期（DayofMonth）	1~31的整数（但是你需要考虑你月的天数）	,- * ? / L W C     八个字符
    月份（Month）	1~12的整数或者 JAN-DEC	, - * /    四个字符
    星期（DayofWeek）	1~7的整数或者 SUN-SAT （1=SUN）	, - * ? / L C #     八个字符
    年(可选，留空)（Year）	1970~2099	, - * /    四个字符
     */
    public static String getCronExpress(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(calendar.get(Calendar.SECOND)).append(" ")
                .append(calendar.get(Calendar.MINUTE)).append(" ")
                .append(calendar.get(Calendar.HOUR_OF_DAY)).append(" ")
                .append(calendar.get(Calendar.DATE)).append(" ")
                .append(calendar.get(Calendar.MONTH) + 1).append(" ")
                //.append(calendar.get(Calendar.DAY_OF_WEEK)).append(" ")
                .append("* ")
                .append(calendar.get(Calendar.YEAR));
        return stringBuilder.toString();
    }

    //构建cron表达式
    public static String getCronExpress(Integer month, Integer day, Integer hour, Integer minutes) {
        if (month == null && day == null && hour == null && minutes == null) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("* ");
        if (minutes == null) {
            stringBuilder.append("* ");
        } else {
            stringBuilder.append(minutes).append(" ");
        }
        if (hour == null) {
            stringBuilder.append("* ");
        } else {
            stringBuilder.append(hour).append(" ");
        }
        if (day == null) {
            stringBuilder.append("* ");
        } else {
            stringBuilder.append(day).append(" ");
        }
        if (month == null) {
            stringBuilder.append("* ");
        } else {
            stringBuilder.append(month).append(" ");
        }
        stringBuilder.append("?");
        return stringBuilder.toString();
    }

    //构建cron表达式的文字描述: * 5 * * * ? *
    public static String getCronDescription(String cron) {
        String[] cronArr = StringUtils.split(cron, " ");
        if (cronArr == null || cronArr.length != 6) {
            return "";
        }

        StringBuilder stringBuilder = new StringBuilder();
        if ("*".equals(cronArr[4]) && "*".equals(cronArr[3]) && "*".equals(cronArr[2])) {
            stringBuilder.append("每时")
                    .append("*".equals(cronArr[1]) ? "每" : cronArr[1]).append("分");
        } else if ("*".equals(cronArr[4]) && "*".equals(cronArr[3])) {
            stringBuilder.append("每日")
                    .append("*".equals(cronArr[2]) ? "每" : cronArr[2]).append("时")
                    .append("*".equals(cronArr[1]) ? "每" : cronArr[1]).append("分");
        } else {
            stringBuilder.append("*".equals(cronArr[4]) ? "每" : cronArr[4]).append("月")
                    .append("*".equals(cronArr[3]) ? "每" : cronArr[3]).append("日")
                    .append("*".equals(cronArr[2]) ? "每" : cronArr[2]).append("时")
                    .append("*".equals(cronArr[1]) ? "每" : cronArr[1]).append("分");
        }
        return stringBuilder.toString();
    }
}
